$(function(){

    var addresses = null;
    $.ajax({
        type: "get",
        url: "/address/queryAddress",
        success: function (res) {
            //console.log(res);
            addresses = res;
           var html = template('addressTpl',{result:res})
           $("#address-box").html(html);
        }
    });


    $("#address-box").on("tap",".delete-btn",function(){
        var id = $(this).attr('data-id');
        var li = $(this).parents("li")[0];
        mui.confirm("确认要删除吗?",function(message){
            if(message.index == 1){
                $.ajax({
                    type: "post",
                    url: "/address/deleteAddress",
                    data: {
                        id : id
                    },
                    success: function (res) {
                        if(res.success) {
                            location.reload();
                        }
                    }
                });
            }else {
                mui.swipeoutClose(li);
            }
        });
    });

    $("#address-box").on("tap",".edit-btn",function(){
        var id = $(this).attr("data-id");
        console.log(id);
        for(var i=0;i<addresses.length;i++){
            if(addresses[i].id == id){
                localStorage.setItem('editAddress',JSON.stringify(addresses[i]));
                console.log(addresses[i]);
                break;
            }
        }
        location.href = "addAddress.html?isEdit=1";
    });

    

});